<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIScreen2
 *
 * @author Administrator
 */
class BUIScreen extends BUIThemedControl {

    public $displayName = null;
    private $controlCount = 0;

    public function load($data) {

        if ($data instanceof BConfig) {
            $screenConfig = $data;
            $name = $screenConfig->getConfigValue('name');
            if ($name != NULL)
                $this->setName($name);
            else {
                $name = 'default_screen';
                if ($screenConfig->getName() != NULL)
                    $name = $screenConfig->getName();
                $this->setName($name);
            }

            $displayName = $screenConfig->getConfigValue('displayName');
            if ($displayName != NULL)
                $this->displayName = $displayName;
            else
                $this->displayName = $this->name;

            $views = $screenConfig->getConfigValue('views');
            if (is_string($views))
                $this->addViews($views);
            if ($views instanceof BConfig) {
                $viewsConfig = $views->getConfigList();
                foreach ($viewsConfig as $viewConfig) {
                    $view = $viewConfig->getValue();
                    $this->addViews($view);
                }
            }
        }

        if (is_array($data)) {

            if (isset($data['name'])) {
                $name = $data['name'];
                $this->setName($name);
            }

            if ($this->name == NULL)
                $this->name = 'startScreen';

            if (isset($data['layoutView']))
                $this->setLayoutView($data['layoutView']);

            if (isset($data['displayName']))
                $this->displayName = $data['displayName'];

            if (isset($data['views'])) {
                $views = $data['views'];
                if (is_string($views)) {
                    $this->addViews($data['views']);
                }
                if (is_array($views)) {
                    foreach ($views as $view) {
                        $this->addViews($view);
                    }
                }
            }
        }
    }

    public function addContextVariable($name, $value, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getContextObj();

        $tObj->addContextVariable(BContextObj::Scope_Screen, $name, $value);
    }

    public function getContextVariable($name, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getContextObj();

        return $tObj->getContextVariable(BContextObj::Scope_Screen, $name);
    }

    public function resetContextVariable($name = NULL) {

        $tObj = Blyn::app()->getContextObj();
        /**
         * for scope reset, only happen when id changed
         */
        if ($name == NULL) {
            $tId = $this->getContextVariable(BContextObj::Current_Id);
            $myId = $this->getId();
            if ($tId != $myId) {
                $tObj->resetContextVariable(BContextObj::Scope_Screen);
                $this->addContextVariable(BContextObj::Current_Id, $myId);
            }
        } else {
            $tObj->resetContextVariable(BContextObj::Scope_Screen, $name);
        }
    }

    public function getAjaxContainerId($data = null) {
        //parent::getAjaxContainerId($data);
        return BUIBase::ajaxContainter_screen;
    }

    public function getAjaxLink($name, $data = array(), $sender = NULL, $route = 'site/index', $updateControl = NULL) {
        if ($this->getNode() != NULL && Blyn::app()->getCurrentNode()->getId() != $this->getNode()->getId())
            return $this->getNode()->getAjaxLink($name, $data, $sender, $router, $updateControl);
        else {
            //if ($updateControl == NULL)
            //$updateControl = Blyn::app()->getCurrentNode()->getUIManager();
            return parent::getAjaxLink($name, $data, $sender, $route, $updateControl);
        }
    }

    public function onRender($data = null) {
        $currentUINode = Blyn::app()->getCurrentNode()->getUIManager();
        $currentUINode->setCurrentScreen($this);
        $views = $this->views;
        if (isset($views['screenTopBar']) && $views['screenTopBar'] == NULL)
            $this->addRenderView('screenTopBar', 'screen_topBar');
    }

    public function getControlCount($data = NULL) {
        $this->controlCount++;
        return $this->controlCount;
    }
    
    
    public function getAngularControllerDefine($data = NULL) {
        return 'bngScreenController';
    }

    /*
      public function getUrl($data = NULL) {
      $ajaxUrl = $this->getAjaxUrl($data);

      if ($ajaxUrl != NULL)
      return $ajaxUrl;

      $spaceId = Blyn::app()->getActiveSpace()->getId();
      $appId = $this->getNode()->getApp()->getId();
      $oppId = $this->getNode()->getId();

      return $this->buildUrl($spaceId, $appId, $oppId, $data);
      }
     * 
     */
}

?>
